home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / DBMSTORAGE.PY < prev    next >
Encoding:
Python Source  |  1999-07-11  |  6.8 KB  |  190 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. """Very Simple dbm-based ZODB storage
  65.  
  66. This storage provides for use of dbm files as storages that
  67. don't support versions or Undo.  This may be useful when implementing
  68. objects like hit counters that don't need or want to participate
  69. in undo or versions.
  70. """
  71. __version__='$Revision: 1.1 $'[11:-2]
  72.  
  73. import base64, POSException, time, string, utils
  74.  
  75. from MappingStorage import MappingStorage
  76. from BaseStorage import BaseStorage
  77. import anydbm, os
  78.  
  79. class anydbmStorage(MappingStorage):
  80.  
  81.     def __init__(self, filename, flag='r', mode=0666):
  82.  
  83.  
  84.         BaseStorage.__init__(self, filename)
  85.         self._index=anydbm.open(filename, flag, mode)
  86.         self._tindex=[]
  87.         keys=self._index.keys()
  88.         if keys: self._oid=max(keys)
  89.  
  90.     def getSize(self):
  91.         # This is a little iffy, since we aren't entirely sure what the file is
  92.         self._lock_acquire()
  93.         try:    
  94.             try:
  95.                 return (os.stat(self.__name__+'.data')[6] +
  96.                         os.stat(self.__name__+'.dir')[6]
  97.                         )
  98.             except:
  99.                 try: return os.stat(self.__name__)[6]
  100.                 except: return 0
  101.         finally: self._lock_release()
  102.  
  103. class gdbmStorage(anydbmStorage):
  104.  
  105.     def __init__(self, filename, flag='r', mode=0666):
  106.  
  107.         BaseStorage.__init__(self, filename)
  108.         import gdbm
  109.         self._index=index=gdbm.open(filename, flag[:1]+'f', mode)
  110.         self._tindex=[]
  111.  
  112.         m='\0\0\0\0\0\0\0\0'
  113.         oid=index.firstkey()
  114.         while oid != None:
  115.             m=max(m, oid)
  116.             oid=index.nextkey(oid)
  117.  
  118.         self._oid=m
  119.  
  120.     def getSize(self):
  121.         self._lock_acquire()
  122.         try: return os.stat(self.__name__)[6]
  123.         finally: self._lock_release()
  124.  
  125.     def pack(self, t, referencesf):
  126.         
  127.         self._lock_acquire()
  128.         try:    
  129.             # Build an index of *only* those objects reachable
  130.             # from the root.
  131.             index=self._index
  132.             rootl=['\0\0\0\0\0\0\0\0']
  133.             pop=rootl.pop
  134.             pindex={}
  135.             referenced=pindex.has_key
  136.             while rootl:
  137.                 oid=pop()
  138.                 if referenced(oid): continue
  139.     
  140.                 # Scan non-version pickle for references
  141.                 r=index[oid]
  142.                 pindex[oid]=r
  143.                 p=r[8:]
  144.                 referencesf(p, rootl)
  145.  
  146.             # Now delete any unreferenced entries:
  147.  
  148.             deleted=[]
  149.             oid=index.firstkey()
  150.             while oid != None:
  151.                 if not referenced(oid): deleted.append(oid)
  152.                 oid=index.nextkey(oid)
  153.  
  154.             pindex=referenced=None
  155.  
  156.             for oid in deleted: del index[oid]
  157.  
  158.             index.sync()
  159.             index.reorganize()
  160.     
  161.         finally: self._lock_release()
  162.  
  163.  
  164.     def _finish(self, tid, user, desc, ext):
  165.  
  166.         index=self._index
  167.         for oid, p in self._tindex: index[oid]=p
  168.         index.sync()
  169.